﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Maticsoft.DBUtility;

namespace ElingOA6_Admin_DSJCJ.DAL
{
    /// <summary>
    /// LinkType数据类
    /// </summary>
    public class LinkType
    {
        /// <summary>
        /// <creator>chenly</creator>
        /// <date>2013-7-15</date>
        /// 获得数据列表
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        /// <returns>DataSet表</returns>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FLinkTypeCD,FLinkTypeName,FEntityNo,FLinkProp ");
            strSql.Append(" FROM t_LinkType ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where  FEntityNo=" + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }


        /// <summary>
        /// 得到一个对象实体
        /// <creator>chenly</creator>
        /// <date>2013-7-15</date>
        /// </summary>
        /// <param name="fLinkTypeCD">类别编号</param>
        /// <param name="fEntityNo">公司编号</param>
        /// <returns>一个对象实体</returns>
        public Model.LinkType GetModel(string fLinkTypeCD, string fEntityNo)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 FLinkTypeCD,FEntityNo,FLinkTypeName,FLinkProp from t_LinkType ");
            strSql.Append(" where FLinkTypeCD=@FLinkTypeCD and FEntityNo=@FEntityNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FLinkTypeCD", SqlDbType.NVarChar,50),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,50)};
            parameters[0].Value = fLinkTypeCD;
            parameters[1].Value = fEntityNo;

            Model.LinkType model = new Model.LinkType();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.FLinkTypeCD = ds.Tables[0].Rows[0]["FLinkTypeCD"].ToString();
                model.FEntityNo = ds.Tables[0].Rows[0]["FEntityNo"].ToString();
                model.FLinkTypeName = ds.Tables[0].Rows[0]["FLinkTypeName"].ToString();
                model.FLinkProp = ds.Tables[0].Rows[0]["FLinkProp"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 是否存在该记录
        /// <creator>chenly</creator>
        /// <date>2013-7-15</date>
        /// </summary>
        /// <param name="fLinkTypeCD">类别编号</param>
        /// <param name="fEntityNo">公司编号</param>
        /// <returns>是否存在</returns>
        public bool Exists(string fLinkTypeCD, string fEntityNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from t_LinkType");
            strSql.Append(" where FLinkTypeCD=@FLinkTypeCD and FEntityNo=@FEntityNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FLinkTypeCD", SqlDbType.NVarChar,50),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,50)};
            parameters[0].Value = fLinkTypeCD;
            parameters[1].Value = fEntityNo;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        /// <creator>chenly</creator>
        /// <date>2013-7-15</date>
        /// <param name="model">model对象</param>
        public void Add(Model.LinkType model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into t_LinkType(");
            strSql.Append("FLinkTypeCD,FEntityNo,FLinkTypeName,FLinkProp)");
            strSql.Append(" values (");
            strSql.Append("@FLinkTypeCD,@FEntityNo,@FLinkTypeName,@FLinkProp)");
            SqlParameter[] parameters = {
					new SqlParameter("@FLinkTypeCD", SqlDbType.NVarChar,6),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FLinkTypeName", SqlDbType.NVarChar,40),
					new SqlParameter("@FLinkProp", SqlDbType.NChar,1)};
            parameters[0].Value = model.FLinkTypeCD;
            parameters[1].Value = model.FEntityNo;
            parameters[2].Value = model.FLinkTypeName;
            parameters[3].Value = model.FLinkProp;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        /// <creator>chenly</creator>
        /// <date>2013-7-15</date>
        /// <param name="model">model对象</param>
        public bool Update(ElingOA6_Admin_DSJCJ.Model.LinkType model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_LinkType set ");
            strSql.Append("FLinkTypeName=@FLinkTypeName,");
            strSql.Append("FLinkProp=@FLinkProp");
            strSql.Append(" where FLinkTypeCD=@FLinkTypeCD and FEntityNo=@FEntityNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FLinkTypeName", SqlDbType.NVarChar,40),
					new SqlParameter("@FLinkProp", SqlDbType.NChar,1),
					new SqlParameter("@FLinkTypeCD", SqlDbType.NVarChar,6),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)};
            parameters[0].Value = model.FLinkTypeName;
            parameters[1].Value = model.FLinkProp;
            parameters[2].Value = model.FLinkTypeCD;
            parameters[3].Value = model.FEntityNo;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        /// <creator>chenly</creator>
        /// <date>2013-7-15</date>
        /// <param name="fLinkTypeCD">类别编号</param>
        /// <param name="fEntityNo">公司编号</param>
        public void Del(string fLinkTypeCD, string fEntityNo)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_LinkType ");
            strSql.Append(" where FLinkTypeCD=@FLinkTypeCD and FEntityNo=@FEntityNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FLinkTypeCD", SqlDbType.NVarChar,50),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,50)};
            parameters[0].Value = fLinkTypeCD;
            parameters[1].Value = fEntityNo;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 获得数据列表
        /// <creator>wangl</creator>
        /// <date>2013-09-09</date>
        /// </summary>
        public DataSet GetAllList(string entityNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FLinkTypeCD,FEntityNo,FLinkTypeName,FLinkProp ");
            strSql.Append(" FROM t_LinkType ");
            strSql.Append(" where FEntityNo=@FEntityNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)			};
            parameters[0].Value = entityNo;
            return DbHelperSQL.Query(strSql.ToString(), parameters);
        }


    }
}
